home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Simple_Module2 / modinit.c < prev    next >
C/C++ Source or Header  |  1998-08-25  |  6KB  |  214 lines

  1.  
  2. /********************************************************************
  3.  
  4.  modinit.c - standard initialisation for Opus 5 modules
  5.              ( ... slightly changed by me ... :-) )
  6.  
  7.  Typically it is not required for you to change here anything,
  8.  only if you use a different compiler...
  9.  In this case you may refer also to CLib37.lha (AmiNet).
  10.  
  11.  If your module does require the datatypes library or
  12.  rexxsyslib.library, just define DATATYPES or AREXX in the compiler
  13.  options of the smakefile.
  14.  
  15.  Function: It does open/close the libraries you may need (add yours,
  16.  if they are not here) and make them available for the whole module.
  17.  
  18.  ********************************************************************/
  19.  
  20. #ifndef _DOPUS_MODULE_DEF
  21. #define _DOPUS_MODULE_DEF
  22. #include <dopus/modules.h>
  23. #endif
  24.  
  25. #ifndef CLIB_EXEC_PROTOS_H
  26. #include <clib/exec_protos.h>
  27. #include <pragmas/exec_pragmas.h>
  28. #endif
  29.  
  30. #ifndef EXEC_MEMORY_H
  31. #include <exec/memory.h>
  32. #endif
  33.  
  34. #ifndef CLIB_LOCALE_PROTOS_H
  35. #include <clib/locale_protos.h>
  36. #include <pragmas/locale_pragmas.h>
  37. #endif
  38.  
  39. /********************************************************************/
  40.  
  41. // some prototypes for the functions here
  42. // needed by SAS to create the library header
  43. int  __saveds __UserLibInit( void );
  44. void __saveds __UserLibCleanup( void );
  45.  
  46. struct Library *DOSBase;
  47. struct Library *AslBase;
  48. struct Library *GfxBase;
  49. struct Library *DOpusBase;
  50. struct Library *LocaleBase;
  51. struct Library *UtilityBase;
  52. struct Library *DiskfontBase;
  53. struct Library *GadToolsBase;
  54. struct Library *IntuitionBase;
  55.  
  56. #ifdef DATATYPES
  57. struct Library *DataTypesBase;
  58. #endif
  59. #ifdef AREXX
  60. struct Library *RexxSysBase; 
  61. #endif
  62.  
  63. // our module memorypool
  64. APTR mempool;
  65.  
  66. // Locale pointer
  67. struct DOpusLocale *locale;
  68.  
  69. // a prototype from buildinstrings.c
  70. extern void init_locale_data(struct DOpusLocale *locale);
  71.  
  72. /********************************************************************/
  73.  
  74. // Library initialisation code
  75. int __saveds __UserLibInit()
  76. {
  77.         // Initialise pointers
  78.         AslBase = 0;
  79.         GfxBase = 0;
  80.         DOpusBase = 0;
  81.         LocaleBase = 0;
  82.         UtilityBase = 0;
  83.         DiskfontBase = 0;
  84.         GadToolsBase = 0;
  85.         IntuitionBase = 0;
  86.         locale = 0;
  87.         mempool = NULL;
  88.                   
  89. #ifdef DATATYPES                  
  90.         DataTypesBase = 0;
  91. #endif            
  92. #ifdef AREXX
  93.         RexxSysBase = 0;
  94. #endif
  95.                                  
  96.         // Get DOS library (can't really fail)
  97.         DOSBase = OpenLibrary( "dos.library", 0 );
  98.  
  99.         // Open other libraries we need
  100.         if( !(AslBase = OpenLibrary("asl.library", 37))              ||
  101.             !(GfxBase = OpenLibrary("graphics.library", 37))         ||
  102.             !(DOpusBase = OpenLibrary("dopus5.library", 55))         ||
  103.             !(UtilityBase = OpenLibrary("utility.library", 37))      ||            
  104.             !(GadToolsBase = OpenLibrary( "gadtools.library", 37))   ||
  105.             !(IntuitionBase = OpenLibrary( "intuition.library", 37)) ||
  106.                                 
  107. #ifdef DATATYPES                                
  108.             !(DataTypesBase = OpenLibrary( "datatypes.library", 39)) ||
  109. #endif                          
  110. #ifdef AREXX
  111.             !(RexxSysBase = OpenLibrary("rexxsyslib.library", 0))    ||
  112. #endif      
  113.             !(DiskfontBase = OpenLibrary("diskfont.library", 37)) )
  114.              return 1;
  115.         
  116.         // Creating our memorypool and use it immediate for the locale
  117.         if( !(mempool = NewMemHandle(4096, 3072, MEMF_CLEAR|MEMF_PUBLIC)) ||
  118.             !(locale = AllocMemH(mempool, sizeof(struct DOpusLocale))) )
  119.              return 1;
  120.  
  121.         init_locale_data(locale);
  122.  
  123.         // Open locale library
  124.         if( LocaleBase = OpenLibrary("locale.library", 38) )
  125.           {
  126.              // Store library pointer
  127.              locale->li_LocaleBase = LocaleBase;
  128.  
  129.              // Open catalog if name supplied
  130.              if( module_info.locale_name )
  131.                {
  132.                   struct TagItem tags[2];
  133.  
  134.                   // If MODULEF_CATALOG_VERSION is set, we do version checking
  135.                   tags[0].ti_Tag = (module_info.flags & MODULEF_CATALOG_VERSION) ? OC_Version : TAG_IGNORE;
  136.                   tags[0].ti_Data = module_info.ver;
  137.                   tags[1].ti_Tag = TAG_DONE;
  138.  
  139.                   // Open catalog
  140.                   locale->li_Catalog = OpenCatalogA( NULL, module_info.locale_name, tags );
  141.                }
  142.  
  143.              // Get default lolale
  144.              locale->li_Locale = OpenLocale( 0 );
  145.           }
  146.         
  147.         return NULL; // Succeeded
  148. }
  149.  
  150.  
  151. // Clean up
  152. void __saveds __UserLibCleanup()
  153. {
  154.      if( mempool )
  155.        {
  156.           if( locale )
  157.             {
  158.                if( LocaleBase )
  159.                  {
  160.                     CloseLocale( locale->li_Locale );
  161.                     CloseCatalog( locale->li_Catalog );
  162.                     CloseLibrary( LocaleBase );
  163.                  }
  164.  
  165.                FreeMemH(locale);
  166.             }
  167.  
  168.           FreeMemHandle( mempool );
  169.        }
  170.                   
  171.      // Close libraries
  172.      CloseLibrary( AslBase );
  173.      CloseLibrary( GfxBase );
  174.      CloseLibrary( DOpusBase );
  175.      CloseLibrary( DiskfontBase );
  176.      CloseLibrary( UtilityBase );
  177.                   
  178. #ifdef DATATYPES
  179.      CloseLibrary( DataTypesBase );
  180. #endif            
  181. #ifdef AREXX              
  182.      CloseLibrary( RexxSysBase );
  183. #endif        
  184.  
  185.      CloseLibrary( GadToolsBase );
  186.      CloseLibrary( IntuitionBase );
  187.      CloseLibrary( DOSBase );
  188. }
  189.  
  190. /********************************************************************/
  191.  
  192. // This routine is called by DOpus to find out what the module does
  193. // Do not modify it or move it to an other place !!
  194.  
  195. ModuleInfo *__asm __saveds L_Module_Identify( register __d0 int num )
  196. {
  197.      // Return module information
  198.      if( num == -1 )
  199.           return &module_info;
  200.  
  201.      // Valid function number?
  202.      if( num > module_info.function_count ||
  203.          !(module_info.function[num].desc) )
  204.           return 0;
  205.  
  206.      // Return function description
  207.      return (ModuleInfo *) DOpusGetString( locale, module_info.function[num].desc );
  208. }
  209.  
  210. /********************************************************************/
  211.  
  212.  
  213.  
  214.